<?php

namespace app\admin\model;

use think\Model;

class HousingResources extends Model
{
    // 设置当前模型对应的完整数据表名称
    protected $table = 'ty_housing_resources';

    //指定主见
    protected $pk = 'hr_id';

    //只读字段
    //protected $readonly = ['name','email'];

    //自动写入时间戳
    protected $autoWriteTimestamp = true;

    // 定义时间戳字段名
    protected $createTime = 'hr_create_time';
    protected $updateTime = 'con_update_time';

    //自动写入日期
    //protected $autoWriteTimestamp = 'datetime';

    // 设置返回数据集的对象名
    protected $resultSetType = 'collection';

   protected $type = [
        'hr_id'                 =>  'string',
    ];


    /**
     * 房源列表
     */
   public static function aselect($where = [])
   {
       $page = $_GET['page'] ?? 1;

        return  self::where($where)
            ->join('ty_admin_admins','ty_admin_admins.id = ty_housing_resources.hr_sales_id','left')                    //房源
            ->join('ty_channel_member','ty_channel_member.cm_id = ty_housing_resources.hr_cm_id','left')                //渠道信息
            ->join('ty_business_circle','ty_business_circle.bc_id = ty_housing_resources.hr_bc_id','left')              //商圈信息
            ->join('ty_residential_quarters','ty_residential_quarters.rq_id = ty_housing_resources.hr_rq_id','left')    //小区
            ->Field(
                'ty_housing_resources.*,
                ty_admin_admins.truename,
                ty_channel_member.cm_name,
                ty_business_circle.bc_name,
                ty_residential_quarters.rq_name')
            ->order('hr_id','desc')
            ->paginate(10, false, ['page' => $page,'query'=>request()->param()]);
   }

    /**
     * 房源列表
     */
    public static function adetail($where)
    {
        return self::where($where)
            ->join('ty_admin_admins','ty_admin_admins.id = ty_housing_resources.hr_sales_id','left')
            ->join('ty_channel_member','ty_channel_member.cm_id = ty_housing_resources.hr_cm_id','left')
            ->join('ty_business_circle','ty_business_circle.bc_id = ty_housing_resources.hr_bc_id','left')
            ->join('ty_residential_quarters','ty_residential_quarters.rq_id = ty_housing_resources.hr_rq_id','left')
            ->Field(
                'ty_housing_resources.*,
                ty_admin_admins.truename,
                ty_channel_member.cm_name,
                ty_business_circle.bc_name,
                ty_residential_quarters.rq_name')
            ->find();
    }


    /**
     * 房源实勘获取器
     * @param $value
     * @return mixed
     */
/*    public function getHrStatusAttr($value)
    {
        $status = [1=>'未跟进',2=>'已联系',3=>'已实勘',4=>'风控处理',5=>'可签约',6=>'已签约',7=>'已失效',8=>'溢出状态'];
        if (!isset($status[$value])) {
            return '暂无';
        }
        return $status[$value];
    }*/

    /**
     * 房源实勘获取器
     * @param $value
     * @return mixed
     */
    public function getHrStatusTextAttr($value,$data)
    {
        $status = [1=>'未跟进',2=>'已联系',3=>'已实勘',4=>'风控处理',5=>'可签约',6=>'已签约',7=>'已失效',8=>'溢出状态'];

        if (!isset($status[$data['hr_status']])) {
            return '暂无';
        }

        return $status[$data['hr_status']];

    }
    /**
     * 房源类型获取器
     * @param $value
     * @return mixed
     */
    public function getHrTypeTextAttr($value,$data)
    {
        $status = [1=>'公寓类型',2=>'民宅平层',3=>'民宅复式'];

        if (!isset($status[$data['hr_type']])) {
            return '暂无';
        }

        return $status[$data['hr_type']];
    }



    /**
     * 添加房源
     */
    public static function zadd($data)
    {
        return self::create($data);

    }

    public static function zsave($data)
    {
        $Hr = new HousingResources();
        return $Hr->save($data,['hr_id'=>$data['hr_id']]);

    }



    /**
     * 房源列表
     * @param $sales_id 业务员id
     * @return array|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function zselect($where,$where2)
    {
        if ($where2) {
            return self::where($where)
                ->where(function($q) use ($where2){
                    $q->whereOr($where2);
                })
                ->join('ty_risk_management','ty_risk_management.hr_id = ty_housing_resources.hr_id','left')
                ->order('rm_id','desc')
                ->Field('ty_housing_resources.*,rm_status')
                ->group('ty_housing_resources.hr_id')
                ->paginate(10);
        } else {
            return self::where($where)
                ->join('ty_risk_management','ty_risk_management.hr_id = ty_housing_resources.hr_id','left')
                ->order('rm_id','desc')
                ->Field('ty_housing_resources.*,rm_status')
                ->group('ty_housing_resources.hr_id')
                ->paginate(10);
        }

    }

    public static function rqidSelect($rq_id)
    {
        return self::where('hr_rq_id',$rq_id)->paginate(10);
    }


    public static function zget($hr_id)
    {
        return self::where('hr_id',$hr_id)->find();
    }

    /**
     * 查看是否具备实勘条件
     */
    public static function prospectingCheck($hr_id)
    {
        return self::where(['hr_id'=>$hr_id,'hr_status'=>[2,3]])->find();
    }

    /**
     * 溢出房源
     * @return array|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function noBindSales($data)
    {
        return self::where($data)->paginate(10);
    }

    public static function bingCheck($hr_id)
    {
        return self::where(['hr_id'=>$hr_id,'hr_sales_id'=>0])->find();
    }

    /**
     * 溢出房源抢单
     * @param $data
     * @return HousingResources
     */
    public static function bindSales($data)
    {
        return self::update($data);
    }

}
